package com.fita.admin.service.client.impl;

import com.demo2do.core.entity.exception.BusinessException;
import com.fita.admin.entity.search.client.ClientInterviewRecordSearch;
import com.fita.admin.entity.security.Login;
import com.fita.admin.service.DataServiceImpl;
import com.fita.admin.service.client.ClientInterviewRecordService;
import com.fita.entity.client.Client;
import com.fita.entity.client.ClientInterviewRecord;
import com.fita.entity.principal.Principal;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description:
 * @author: 11200
 * @date: 2019/6/21
 */
@Service("clientInterviewService")
public class ClientInterviewRecordServiceImpl extends DataServiceImpl<ClientInterviewRecord> implements ClientInterviewRecordService {

    /**
     * @param client
     * @return
     */
    public List<ClientInterviewRecord> loadByClient(Client client) {

        List<ClientInterviewRecord> clientInterviewRecords = genericDaoSupport.searchForList("FROM ClientInterviewRecord WHERE disabled = false AND client = :client", "client", client);

        return clientInterviewRecords;
    }

    /**
     * @param clientId
     * @param clientInterviewRecord
     * @param login
     */
    public void add(Long clientId, ClientInterviewRecord clientInterviewRecord, Login login) {

        Client client = genericDaoSupport.load(Client.class, clientId);

        Principal principal = genericDaoSupport.load(Principal.class, login.getId());

        clientInterviewRecord.create(client, principal);

        genericDaoSupport.save(clientInterviewRecord);
    }

    /**
     * @param clientInterviewRecordId
     */
    public void delete(Long clientInterviewRecordId) throws BusinessException {

        ClientInterviewRecord clientInterviewRecord = genericDaoSupport.load(ClientInterviewRecord.class, clientInterviewRecordId);

        clientInterviewRecord.delete();

        genericDaoSupport.update(clientInterviewRecord);
    }

    /**
     * @param clientInterviewRecordId
     * @param clientInterviewRecord
     */
    public void edit(Long clientInterviewRecordId, ClientInterviewRecord clientInterviewRecord) throws BusinessException {

        ClientInterviewRecord load = genericDaoSupport.load(ClientInterviewRecord.class, clientInterviewRecordId);
        if (load == null){
            throw BusinessException.newInstance("500", "客户访谈不存在");
        }
        load.edit(clientInterviewRecord);
        genericDaoSupport.update(load);

    }

    /**
     * @param search
     * @return
     */
    public List<ClientInterviewRecord> search(ClientInterviewRecordSearch search) {

        List list = genericDaoSupport.searchForList(search.buildSentenceTemplate(), search.buildParameters());

        return list;

    }
}
